const getBalanceRandom = (moneyAmount: number, personCount: number) => {
  if (personCount === 1) {
    return moneyAmount;
  }

  const average = moneyAmount / personCount;
  const max = average * 3;
  const min = 0.01;

  const random = Math.random() * max;

  // 因为max是平均值的3倍，当人数小于3人时，可能会大于剩余金额，所以需要用Math.min来进行约束
  return Math.min(random, moneyAmount - (personCount - 1) * min);
};

function distributeMoney(moneyAmount: number, personCount: number) {
  const result: number[] = [];

  let leftMoney = moneyAmount;
  let leftPersonCount = personCount;

  for (let i = 0; i < personCount; i++) {
    const balance = getBalanceRandom(leftMoney, leftPersonCount);
    result.push(balance);
    leftMoney -= balance;
    leftPersonCount--;
  }

  return result;
}

console.log(distributeMoney(100, 10));